package com.jinlejia.erp.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.databinding.ItemSellingLayoutBinding;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;

/**
 * 选择买卖房源Adapter
 */
public class HouseSelectAdapter extends YfListAdapter<Map<String, Object>> {


    private Context context;
    private String showBtn;
    private HashSet<String> IDS = new HashSet<>();

    public HouseSelectAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    public HouseSelectAdapter(ArrayList<Map<String, Object>> data, String showbtn) {
        super(data);
        this.showBtn = showbtn;
    }

    public void setSelectHouseID(HashSet<String> IDs) {
        this.IDS = IDs;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selling_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        final ItemSellingLayoutBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);

        Map<String, Object> map = mData.get(i);
        if (IDS.contains(map.get("rscode"))) {
            binding.checkbox.setChecked(true);
            if (dListiner != null)
                dListiner.onDefault(map);
        } else {
            binding.checkbox.setChecked(false);
        }
        if (map.containsKey("image"))
            Glide.with(context).load(ApiConfig.MAGEHEADER_URL() + map.get("image")).placeholder(R.mipmap.ic_no_img).error(R.mipmap.ic_no_img).into(binding.ivImage);
        //小区名
        if (map.containsKey("estate"))
            binding.tvHouseName.setText(map.get("estate").toString());
        //商圈
        if (map.containsKey("area")) {
            if (map.containsKey("county")) {
                binding.tvHouseDistrict.setText(map.get("county").toString() + " - " + map.get("area").toString());
            } else {
                binding.tvHouseDistrict.setText(map.get("area").toString());
            }
        }
        //户型
        if (map.containsKey("houseType")) {
            binding.tvHouseType.setText(map.get("houseType").toString());
            if (TextUtils.isEmpty(mData.get(i).get("houseType").toString())) {
                binding.tvHouseType.setVisibility(View.GONE);
            } else {
                binding.tvHouseType.setVisibility(View.VISIBLE);
            }
        }

        //面积
        if (map.containsKey("square"))
            binding.tvHouseArea.setText(map.get("square") + "㎡");
        //均价
        if (map.containsKey("unitPrice")) {
            binding.tvHouseAveragePrice.setText(map.get("unitPrice") + "元/㎡");
            binding.tvHouseAveragePrice.setVisibility(View.VISIBLE);
        } else {
            binding.tvHouseAveragePrice.setVisibility(View.GONE);
        }
        //售价
        if (map.containsKey("salePrice"))
            binding.tvHouseTotle.setText(map.get("salePrice") + "万元");
        if (map.containsKey("rentPrice"))
            binding.tvHouseTotle.setText(map.get("rentPrice") + "元/月");

        if (!TextUtils.isEmpty(showBtn) && showBtn.equals("DEL")) {
            binding.btDelete.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(showBtn) && showBtn.equals("CHECK")) {
            binding.checkbox.setVisibility(View.VISIBLE);
            if (IDS.contains(map.get("rscode"))) {
                binding.checkbox.setChecked(true);
            } else {
                binding.checkbox.setChecked(false);
            }

        }
        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (IDS.contains(mData.get(i).get("rscode"))) {
                    binding.checkbox.setChecked(false);
                    IDS.remove(mData.get(i).get("rscode"));
                    if (rListiner != null) {
                        rListiner.onRemove(mData.get(i));
                    }
                } else {
                    binding.checkbox.setChecked(true);
                    IDS.add(mData.get(i).get("rscode").toString());
                    if (listiner != null) {
                        listiner.Onselected(mData.get(i));
                    }
                }

            }
        });
        binding.getRoot().setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = (TextView) itemView.findViewById(R.id.txt);
            progress = (AVLoadingIndicatorView) itemView.findViewById(R.id.progress);
        }
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            ItemSellingLayoutBinding binding = DataBindingUtil.bind(itemView);
            binding.checkbox.setVisibility(View.VISIBLE);
        }
    }

    public interface OnSelectedListiner {
        void Onselected(Map<String, Object> map);
    }

    private OnSelectedListiner listiner;

    public void setOnSelectedListiner(OnSelectedListiner l) {
        this.listiner = l;
    }

    public interface OnRemoveListiner {
        void onRemove(Map<String, Object> map);
    }

    private OnRemoveListiner rListiner;

    public void setOnRemoveListiner(OnRemoveListiner l) {
        this.rListiner = l;
    }

    public interface OnDefaultSelectListiner {
        void onDefault(Map<String, Object> map);
    }

    private OnDefaultSelectListiner dListiner;

    public void setOnDefaultListiner(OnDefaultSelectListiner l) {
        this.dListiner = l;
    }

}
